<?php
class AdminManage
{
    var $pdo = null;
    var $logger = null;
    function __construct()
    {
        $this->pdo = PDOWrapper::instance();
        $this->logger = CLog::getInstance();
    }


    /**
     * @return array|bool
     * 
     * 管理员修改功能接口
     */
    function ModifyV1($GetParams, $PostArray){

        if(!isset($PostArray['modify_type'])){
            Utils::ShowError(400,'修改类型为空');
            return false;
        }

        $admin_id = self::CheckParams($PostArray);
        if(false === $admin_id) {
            return false;
        }

        $modify = new AdminModify();
        $method = get_class_methods($modify);
        $function = self::CheckMethod($method,$PostArray['modify_type']);
        if($function === false){
            return false;
        }

        $descri = $modify->$function($PostArray); 

        if($descri === false) {
            return false;
        }  

        if(empty($descri)) {
            Utils::ShowError(4000,'没有任何修改！');
            return false;
        }   

        $res = self::RecordDescri($PostArray,$admin_id);
        if(empty($res)){
            return false;
        }

        Utils::ShowMessage();
        return true;
    }

    /**
     * @return array|bool
     * 
     * 管理员添加功能接口
     */
    function AddV1($GetParams, $PostArray){

        if(!isset($PostArray['add_type'])){
            Utils::ShowError(401,'添加类型为空');
            return false;
        }

        $admin_id = self::CheckParams($PostArray);
        if(false === $admin_id) {
            return false;
        }

        $add = new AdminAdd();
        $method = get_class_methods($add);
        $function = self::CheckMethod($method,$PostArray['add_type']);
        if($function === false){
            return false;
        }

        $descri = $add->$function($PostArray);

        if($descri === false) {
            return false;
        }  

        if(empty($descri)) {
            return false;
        }   

        $res = self::RecordDescri($PostArray,$admin_id);
        if(empty($res)){
            return false;
        }

        Utils::ShowMessage();
        return true;
    }

    /**
     * @return array|bool
     * 
     * 管理员查看接口
     */
    function SearchV1($GetParams, $PostArray){

        if(!isset($PostArray['search_type'])){
            Utils::ShowError(402,'查询类型为空');
            return false;
        }

        $admin_id = self::CheckParams($PostArray);
        if(false === $admin_id) {
            return false;
        }

        $search = new AdminSearch();
        $method = get_class_methods($search);
        $function = self::CheckMethod($method,$PostArray['search_type']);
        if($function === false){
            return false;
        }

        $descri = $search->$function($PostArray);

        if($descri === false) {
            return false;
        }  

        if(empty($descri)) {
            return false;
        }   

        $res = self::RecordDescri($PostArray,$admin_id);
        if(empty($res)){
            return false;
        }

        Utils::ShowMessage($descri);
        return true;
    }

    /**
     * @return array|bool
     * 
     * 管理员删除功能接口
     */
    function DeleteV1($GetParams, $PostArray){

        if(!isset($PostArray['delete_type'])){
            Utils::ShowError(403,'删除类型为空');
            return false;
        }

        $admin_id = self::CheckParams($PostArray);
        if(false === $admin_id) {
            return false;
        }

        $delete = new AdminDelete();
        $method = get_class_methods($delete);
        $function = self::CheckMethod($method,$PostArray['delete_type']);
        if($function === false){
            return false;
        }

        $descri = $delete->$function($PostArray);
        
        if($descri === false) {
            return false;
        }  

        if(empty($descri)) {
            return false;
        }   

        $res = self::RecordDescri($PostArray,$admin_id);
        if(empty($res)){
            return false;
        }

        Utils::ShowMessage();
        return true;
    }

    /**
    * 插入数据库新功能，分配新部门功能
    */
    function UpdateV1($GetParams, $PostArray){

        if(!isset($PostArray['token'])) {
            Utils::ShowError(ErrorDefine::SHORT_OF_TOKEN);
            return false;
        }
        $token = $PostArray['token'];
        $admin = User::GetAidByToken($token);

        if($admin === false) {
            Utils::ShowError(ErrorDefine::INVALID_TOKEN);
            return false;
        }
        $admin_id = $admin['admin_id'];
        if($admin['admin_type'] != 1000){
            Utils::ShowError('404','权限不够');
            return false;
        }

        $function = array(
            'search_AdminList'  =>      1,
            'search_AgentList'  =>      2,
            'search_UserList'   =>      3,
        );

        $field = ['dept_id'];
        $order_by = ['dept_id' => 'DESC'];
        $dept = $this->pdo->select('department',$field,null,1,null,$order_by);
        $dept = $dept['dept_id'];
        
        $field = ['auth_id'];
        $order_by = ['auth_id' => 'DESC'];
        $auth_id = $this->pdo->select('auth',$field,null,1,null,$order_by);
        $auth_id = $auth_id['auth_id'];

        if($dept>$auth_id && !empty($auth_id)){
            foreach ($function as $key => $value) {
                $auth_id = $dept + $value;
                $params = [
                    'auth_id' => $auth_id,
                    'auth' => 0
                ];
                $auth = $this->pdo->insert('auth',$params);
            }
        }

        $newfunction = self::UpdateFunction($function);
        if($newfunction === false){
            return false;
        }

        Utils::ShowMessage();
        return true;
    }

    private function UpdateFunction($function){

        $newfunction = [];
        $field = ['func_id'];
        $order_by = ['func_id' => 'DESC'];
        $func_id = $this->pdo->select('function',$field,null,1,null,$order_by);
        $func_id = $func_id['func_id'];
        $last_func = array_values(array_slice($function, -1));
        $new_func = $last_func[0]-$func_id;
        
        if($new_func == 0){
            Utils::ShowError('405','无需更新');
            return false;
        }
        $newfunction = array_splice($function,-$new_func);

        foreach ($newfunction as $key => $value) {
            $param = [
                'func_name' => $key
            ];
            $func_ret = $this->pdo->insert('function',$param);
            if(empty($func_ret)){
                Utils::ShowError(ErrorDefine::SYSTEM_ERROR);
                return false;
            }

        }

        $field = ['dept_id'];
        $dept = $this->pdo->select('department',$field);

        foreach ($dept as $key => $value) {
            foreach ($newfunction as $k => $v) {
                $auth_id = $value['dept_id'] + $v;
                switch ($value['dept_id']) {
                    case 1000:
                        $auth = 1;
                        break;
                    
                    default:
                        $auth = 0;
                        break;
                }
            
                $params = [
                    'auth_id' => $auth_id,
                    'auth' => $auth
                ];
                $auth_ret = $this->pdo->insert('auth',$params);
            }
        }

        return $newfunction;
    }

    /**
    * 检查管理员是否登录以及管理员访问权限
    */
    private function CheckParams($PostArray){
        
        if(!isset($PostArray['token'])) {
            Utils::ShowError(ErrorDefine::SHORT_OF_TOKEN);
            return false;
        }
        $token = $PostArray['token'];
        $admin = User::GetAidByToken($token);
        
        if($admin === false) {
            Utils::ShowError(ErrorDefine::INVALID_TOKEN);
            return false;
        }
        $admin_id = $admin['admin_id'];
        $beh_type = self::CheckBType($PostArray);

        $auth = self::Auth($admin['admin_type'],$beh_type);
        if($auth === false){
            return false;
        }
        
        return $admin_id;
    }

    private function CheckBType($PostArray){

        if(isset($PostArray['search_type'])) {
            $type = 'search_type';
            $content = $PostArray['search_type'];
        } 
        elseif (isset($PostArray['add_type'])) {
            $type = 'add_type';
            $content = $PostArray['add_type'];
        } 
        elseif (isset($PostArray['modify_type'])) {
            $type = 'modify_type';
            $content = $PostArray['modify_type'];
        } 
        elseif (isset($PostArray['delete_type'])) {
            $type = 'delete_type';
            $content = $PostArray['delete_type'];
        }

        $fields = explode("_", $type);
        $beh_type = $fields[0];
        $beh_type = $beh_type.'_'.$content;

        return $beh_type;
    }

    /**
    * 管理员操作成功后生产管理员行为日志
    */
    private function RecordDescri($PostArray,$admin_id){

        $descri = '';
        $add_comma = false;
        foreach ($PostArray as $column => $content) {

            if($column == 'token'){
                continue;
            } 
            elseif ($column == 'modify_type') {
                continue;
            }
            elseif ($column == 'add_type') {
                continue;
            } 
            elseif ($column == 'search_type') {
                continue;
            }
            elseif ($column == 'delete_type') {
                continue;
            }
            elseif ($column == 'picture') {
                continue;
            }

            if ($add_comma) {
                $descri .= ', ';
            }
            else {
                $add_comma = true;
            }

            if( is_array($content) && count($content) > 0 ){
                
                $descri.= $column.' : ';
                $add_comma = false;

                foreach ($content as $k => $v) {
                    if ($add_comma) {
                        $descri .= ', ';
                    }
                    else {
                        $add_comma = true;
                    }
                    $descri .= "$v";
                }
                continue;
            }
            
            $descri .= "$column : $content";
            
        }
        $beh_type = self::CheckBType($PostArray);

        $param = array(
            'admin_id' => $admin_id,
            'beh_type' => $beh_type,
            'remark' => $descri
        );

        $res = $this->pdo->insert('admin_behavior',$param);
        if(empty($res)){
            Utils::ShowError(ErrorDefine::UPDATE_TABLE_FAILED);
            return false;
        }

        return $res;
    }

    private function CheckMethod($method,$type){

        if(in_array($type,$method) === true){
            return $type;
        } else {
            Utils::ShowError(ErrorDefine::INVALID_BUSINESS_TYPE);
            return false;
        }
    }

    /**
    * @param $admin_type 管理员类型
    * @param $beh_type 管理员动作 接口+type
    * @return true 有权限
    * @return false 没有权限
    */
    private function Auth($admin_type,$beh_type){

        if($admin_type == 1000){
            return true;
        }
        $param = [
            'func_name' => $beh_type
        ]; 
        $field = ['func_id'];
        $func_id = $this->pdo->select('function',$field,$param);
        if(empty($func_id)){
            Utils::ShowError(ErrorDefine::INVALID_BUSINESS_TYPE);
            return false;
        }
        $func_id = $func_id[0]['func_id'];

        $auth_id = $admin_type + $func_id;
        $param = [
            'auth_id' => $auth_id
        ];
        $field = ['auth'];
        $auth = $this->pdo->select('auth',$field,$param);

        if($auth[0]['auth'] == 0){
            Utils::ShowError('404','权限不够');
            return false;
        }

        return true;
    }


}


?>